/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Airline;
import easybooking.data.Plane;
import easybooking.data.Type;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author tim
 */
public class PlaneTableModel extends AbstractTableModel {

    private EBInterfaceClient ebic;
    private Vector<Plane> planes;
    private static final int ID_INDEX = 0;
    private static final int NAME_INDEX = 1;
    private static final int TYPEID_INDEX = 2;
    private static final int AIRLINEID_INDEX = 3;
    private static final int OBJECT = 4;
    private String[] columnNames = {"ID", "Name", "Type", "Airline"};

    public PlaneTableModel(Vector<Plane> p, EBInterfaceClient ebifc) {
        this.ebic = ebifc;
        planes = p;
    }
    
/**
 * get the number of rows the table needs
 * @return the number of plane objects in the planes Vector
 */
    public int getRowCount() {
        return planes.size();
    }
/**
 * get the number of columns
 * @return the number of the column Names
 */
    public int getColumnCount() {
        return columnNames.length;

    }

    @Override
            /**
     * get the name of a column
     * @param col is the number of the column which name is wanted
     * @return the name of a column
     */
    public String getColumnName(int col) {
        return columnNames[col];
    }
/**
 * prepares the airline data for the table
 * @param rowIndex the row in which the data get insert
 * @param columnIndex the column in which the data get insert
 * @return the prepared object
 */
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            Plane p = (Plane) planes.get(rowIndex);
            switch (columnIndex) {
                case ID_INDEX:
                    return p.getPlaneId();
                case NAME_INDEX:
                    return p.getName();
                case TYPEID_INDEX:
                    Type t = ebic.findTypeByKey(p.getTypeId());
                    return t.getName();
                case AIRLINEID_INDEX:
                    Airline a = ebic.findAirlineByKey(p.getAirlineId());
                    return a.getName();
                case OBJECT:
                    return p;
                default:
                    return new Object();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(PlaneTableModel.class.getName()).log(Level.SEVERE, null, ex);
            return new Object();
        }
    }



}

